// pages/splash/splash.js
//pages/splash/splash?page=home
const app = getApp()
var util = require('../../utils/util.js')
var that
Page({
  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this;
    //登录code,用户信息
    if (app.globalData.loginStatus == 0) { //请求登录中

      app.loginRequestReadyCallback = res => {
        that.handleJumpTo(options)
      }
    } else {
      that.handleJumpTo(options)
    }
  },
  onShareAppMessage: function(res) {
    return util.handleShareApp()
  },
  handleJumpTo(options) {
    var page = options.page
    if (app.globalData.loginStatus == 2) { //登录成功

      var url = '/pages/index/home/home'

      //获取跳转路径
      if (page) {
        url = that.getJumpUrlByPage(options)
      } else if (options.scene) {
        url = that.getJumpUrlBySense(options.scene)
      }

      try {
        if (url.indexOf('/pages/index/') == 0) {
          wx.switchTab({
            url: url,
          })
        } else {
          wx.redirectTo({
            url: url,
          })
        }
      } catch (e) {
        wx.switchTab({
          url: '/pages/index/home/home',
        })
      }
    } else { //跳转注册or激活页面
      var url = ''
      if (!page) {
        url = '/pages/user/user-home/user-home'
      } else if (page == 'user-register') { //注册
        url = '/pages/user/user-register/user-register'
      } else if (page == 'user-bind') { //绑定
        url = '/pages/user/user-bind/user-bind'
      } else { //其他页面
        url = '/pages/user/user-home/user-home'
      }
      try {
        wx.redirectTo({
          url: url,
        })
      } catch (e) {
        wx.redirectTo({
          url: '/pages/user/user-home/user-home',
        })
      }
    }
  },

  /**
   * 通过page逻辑获取跳转路径
   */
  getJumpUrlByPage(options) {
    var page = options.page
    var url = '/pages/index/home/home'
    if (page == 'home') { //首页
      url = '/pages/index/home/home'
    } else if (page == 'my-rights') { //我的权益
      url = '/pages/index/my/my'
    } else if (page == 'good') { //商品首页
      url = '/pages/index/goods/goods'
    } else if (page == 'goods-detail') { //商品详情
      var goodId = options.goodId
      var skuId = options.skuId
      if (goodId && skuId) {
        url = '/pages/goods/goods-detail/goods-detail?goodId=' + goodId + '&skuId=' + skuId
      } else {
        url = '/pages/index/goods/goods'
      }
    } else if (page == 'order-detail') { //订单详情
      var orderId = options.orderId
      if (orderId) {
        url = '/pages/order/order-detail/order-detail?orderId=' + orderId
      } else {
        url = '/pages/order/my-order/my-order'
      }
    } else if (page == 'logistics') { //物流详情
      var orderId = options.orderId
      if (orderId) {
        url = '/pages/order/logistics/logistics?orderId=' + orderId
      } else {
        url = '/pages/order/my-order/my-order'
      }
    } else if (page == 'field-service-detail') { //现场服务详情
      var appId = options.appId
      var recordId = options.recordId
      if (appId && recordId) {
        url = '/pages/tech/field-service/detail/detail?appid=' + appId + '&recordid=' + recordId
      } else {
        url = '/pages/index/tech/tech'
      }
    } else if (page == 'pd') { //农事指导详情
      var appId = options.a
      var recordId = options.b
      var curTopBarIndex = options.c
      if (appId && recordId) {
        url = '/pages/tech/farm-suggest/detail/detail?appid=' + appId + '&recordid=' + recordId + "&curTopBarIndex=" + curTopBarIndex
      } else {
        url = '/pages/index/tech/tech'
      }
    } else if (page == 'soil-check-detail') { //土壤检测详情
      var appId = options.appId
      var recordId = options.recordId
      if (appId && recordId) {
        url = '/pages/tech/soil-check/detail/detail?appid=' + appId + '&recordid=' + recordId
      } else {
        url = '/pages/index/tech/tech'
      }
    } else if (page == 'fruit-check-detail') { //水果检测详情
      var appId = options.appId
      var recordId = options.recordId
      if (appId && recordId) {
        url = '/pages/tech/fruit-check/detail/detail?appid=' + appId + '&recordid=' + recordId
      } else {
        url = '/pages/index/tech/tech'
      }
    } else if (page == 'savemoney-detail') { //省钱月报详情
      var appId = options.appId
      var recordId = options.recordId
      if (appId && recordId) {
        url = '/pages/tech/savemoney/detail/detail?appid=' + appId + '&recordid=' + recordId
      } else {
        url = '/pages/index/tech/tech'
      }
    } 
    return url;
  },

  /**
   * 通过sense逻辑获取跳转路径
   */
  getJumpUrlBySense(scene) {
    var paramArray = scene.split(";");
    var options = {}
    for (var i = 0; i < paramArray.length; i++) {
      var item = paramArray[i]
      var itemArray = item.split(':');
      var key = itemArray[0];
      var value = itemArray[1];
      options[key] = value
    }

    return that.getJumpUrlByPage(options)
  }

})